package assemblers.beans;

import assemblers.interfaces.AddressAssembler;
import dao.DataAccess;
import infos.AddressInfo;
import model.Address;
import model.Person;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;

@Stateless(name = "AddressAssembler")
public class AddressAssemblerBean implements AddressAssembler {

    DataAccess dataAccess;

    @EJB(beanName = "DataAccess")
    public void setDataAccess(DataAccess dataAccess) {
        this.dataAccess = dataAccess;
    }

    @Override
    public Address assemble(AddressInfo addressInfo) {
        Address address = new Address();
        address.setId(addressInfo.getId());
        address.setApartment(addressInfo.getApartment());
        address.setBuilding(addressInfo.getBuilding());
        address.setCity(addressInfo.getCity());
        address.setStreet(addressInfo.getStreet());
        return address;
    }

    public List<Address> assembleList(List<AddressInfo> infos) {
        List<Address> addressList = new ArrayList<Address>();
        for (AddressInfo a : infos) {
            Address address = assemble(a);
            addressList.add(address);
        }
        return addressList;
    }

    private List<Person> getPersonList(List<Long> personsIdsList) {
        List<Person> persons = new ArrayList<Person>();
        for (Long id : personsIdsList) {
            Person person = dataAccess.find(Person.class, id);
            persons.add(person);
        }
        return persons;
    }
}
